package com.ttg.utils;


import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.io.IOUtils;

import java.io.*;

public abstract class ZipUtils {
    private static final int bufferLen = 8192;

    public ZipUtils() {
    }

    public static byte[] doCompress(byte[] srcData, String zipFileName) throws IOException {
        ZipArchiveOutputStream out = null;

        byte[] var6;
        try {
            ByteArrayOutputStream bout = new ByteArrayOutputStream(8192);
            out = new ZipArchiveOutputStream(bout);
            ZipArchiveEntry entry = new ZipArchiveEntry(zipFileName);
            entry.setSize((long) srcData.length);
            out.putArchiveEntry(entry);
            IOUtils.write(srcData, out);
            out.closeArchiveEntry();
            out.finish();
            byte[] result = bout.toByteArray();
            var6 = result;
        } finally {
            IOUtils.closeQuietly(out);
        }

        return var6;
    }

    public static byte[] doCompress(File srcFile, String zipFileName) throws IOException {
        if (srcFile != null && srcFile.exists() && !srcFile.isDirectory()) {
            ZipArchiveOutputStream out = null;
            BufferedInputStream is = null;

            byte[] var7;
            try {
                is = new BufferedInputStream(new FileInputStream(srcFile), 8192);
                ByteArrayOutputStream bout = new ByteArrayOutputStream(8192);
                out = new ZipArchiveOutputStream(bout);
                ZipArchiveEntry entry = new ZipArchiveEntry(zipFileName);
                entry.setSize(srcFile.length());
                out.putArchiveEntry(entry);
                IOUtils.copy(is, out);
                out.closeArchiveEntry();
                out.finish();
                byte[] result = bout.toByteArray();
                var7 = result;
            } finally {
                IOUtils.closeQuietly(is);
                IOUtils.closeQuietly(out);
            }

            return var7;
        } else {
            throw new IllegalArgumentException("srcFile[" + srcFile.getAbsolutePath() + "] must exist and cannot be a directory.");
        }
    }

    public static void doCompress(File srcFile, File destFile) throws IOException {
        if (srcFile != null && srcFile.exists() && !srcFile.isDirectory()) {
            if (destFile != null && (!destFile.exists() || !destFile.isDirectory())) {
                ZipArchiveOutputStream out = null;
                BufferedInputStream is = null;

                try {
                    is = new BufferedInputStream(new FileInputStream(srcFile), 8192);
                    if (!destFile.exists()) {
                        destFile.createNewFile();
                    }

                    out = new ZipArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), 8192));
                    ZipArchiveEntry entry = new ZipArchiveEntry(srcFile.getName());
                    entry.setSize(srcFile.length());
                    out.putArchiveEntry(entry);
                    IOUtils.copy(is, out);
                    out.closeArchiveEntry();
                    out.finish();
                } finally {
                    IOUtils.closeQuietly(is);
                    IOUtils.closeQuietly(out);
                }

            } else {
                throw new IllegalArgumentException("destFile[" + destFile == null ? "null" : destFile.getAbsolutePath() + "] cannot be empty and cannot be a directory.");
            }
        } else {
            throw new IllegalArgumentException("srcFile[" + srcFile.getAbsolutePath() + "] must exist and cannot be a directory.");
        }
    }

    public static void doDecompressToFile(InputStream src, File destFile) throws IOException {
        ZipArchiveInputStream is = null;

        try {
            is = new ZipArchiveInputStream(new BufferedInputStream(src, 8192));
            Object entry = null;

            while (is.getNextZipEntry() != null) {
                BufferedOutputStream os = null;

                try {
                    os = new BufferedOutputStream(new FileOutputStream(destFile), 8192);
                    IOUtils.copy(is, os);
                } finally {
                    IOUtils.closeQuietly(os);
                }
            }
        } finally {
            IOUtils.closeQuietly(is);
        }

    }

    public static void doDecompress(File srcFile, File destDir) throws IOException {
        if (srcFile != null && srcFile.exists() && !srcFile.isDirectory()) {
            ZipArchiveInputStream is = null;

            try {
                is = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(srcFile), 8192));
                ZipArchiveEntry entry = null;

                while ((entry = is.getNextZipEntry()) != null) {
                    if (entry.isDirectory()) {
                        File os = new File(destDir, entry.getName());
                        os.mkdirs();
                    } else {
                        BufferedOutputStream os1 = null;

                        try {
                            os1 = new BufferedOutputStream(new FileOutputStream(new File(destDir, entry.getName())), 8192);
                            IOUtils.copy(is, os1);
                        } finally {
                            IOUtils.closeQuietly(os1);
                        }
                    }
                }
            } finally {
                IOUtils.closeQuietly(is);
            }

        } else {
            throw new IllegalArgumentException("srcFile[" + srcFile.getAbsolutePath() + "] must exist and cannot be a directory.");
        }
    }
}